<?php

/**
 * milestone actions.
 *
 * @package    mysmartmanager
 * @subpackage milestone
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class milestoneActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->milestones = Doctrine::getTable('Milestone')
      ->createQuery('m')
	  ->where('projectid = ?', $this->getUser()->getAttribute('selectedProject')->getProjectid())
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->milestone = Doctrine::getTable('Milestone')->find(array($request->getParameter('milestoneid')));
	$this->pager = new sfDoctrinePager('Task', sfConfig::get('app_max_tasks_to_show_in_milestone'));
    $this->pager->setQuery($this->milestone->getTasksQuery());
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();	
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MilestoneForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MilestoneForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($milestone = Doctrine::getTable('Milestone')->find(array($request->getParameter('milestoneid'))), sprintf('Object milestone does not exist (%s).', $request->getParameter('milestoneid')));
    $this->form = new MilestoneForm($milestone);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($milestone = Doctrine::getTable('Milestone')->find(array($request->getParameter('milestoneid'))), sprintf('Object milestone does not exist (%s).', $request->getParameter('milestoneid')));
    $this->form = new MilestoneForm($milestone);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($milestone = Doctrine::getTable('Milestone')->find(array($request->getParameter('milestoneid'))), sprintf('Object milestone does not exist (%s).', $request->getParameter('milestoneid')));
    $milestone->delete();

    $this->redirect('milestone/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
     
    if ($form->isValid())
    {
      $this->project = Doctrine::getTable('Project')->find(array($form->getValue('projectid')));
	  if ($form->getValue('startdate') >= $this->project->getStartdate() && $form->getValue('enddate') <= $this->project->getEnddate())
	  {
	    //echo $request->getAttribute('createnewplanversion').'hello';
	    $milestone = $form->save();    
		$milestoneversion = new Milestoneversion();
		$milestoneversion->setMilestoneid($milestone->getMilestoneid());
		$version = $form->getValue('version');
		if ($form->getValue('createnewplanversion') == 1)		
		{
		  //echo $this->getVersion()."--- ".$this->getCreatenewplanversion()."---";
  		  //$this->setValue('version', intval($this->getValue('version'))+1);
		  $version++;
		}
		$milestoneversion->setVersioncode($version) ;
		$milestoneversion->setProjectid($this->project->getProjectid());
		$milestoneversion->setCreatedate(date("Y-m-d H:i:s"));
		$milestoneversion->save();
		
	    if($form->getValue("milestoneid") == 0) {
		  $this->getUser()->setFlash('notice', 'Milestone created successfully.');
		} else {
		  $this->getUser()->setFlash('notice', 'Milestone updated successfully.');
		}
		$this->redirect('milestone/edit?milestoneid='.$milestone->getMilestoneid());
	  }
	  else
	  {
	    $this->getUser()->setFlash('error', 'Milestone dates cannot cross Project dates from '.date("d-m-Y",strtotime($this->project->getStartdate())).' to '.date("d-m-Y",strtotime($this->project->getEnddate())), false);

	  }	  
    }
  }
}
